<?php 
$this->load->view('admin/layout/head', array('title'=>'题目管理','css'=>array('/assets/umeditor/themes/default/css/umeditor.min.css','/assets/ueditor/themes/default/css/ueditor.min.css')), FALSE);
?>

<div id="container">
<ul class="nav nav-tabs" id="subject-tab">
	<li class="active"><a href="javascript:;">添加题目</a></li>
</ul>
<form class="form-inline deal-from container-fluid no-padding" id="question-form" style="margin-top:20px">
	<input type="hidden" name="id" value="<?php echo isset($question['id'])?$question['id']:0 ?>">
	<div id="others-form-div" style="display: none"></div>
	<div id="options-form-div" style="display: none"></div>
	<div class="mt-20">
		<label for="">题目属性</label>
		<div class="form-group">
			<select name="type" class="form-control">
		  		<?php foreach($question_types as $k=>$v): ?>
		  		<option value="<?php echo $k?>" <?php if(isset($question['type'])) echo $k==$question['type']?'selected':'' ?>><?php echo $v ?></option>
		  		<?php endforeach; ?>
		  	</select>
		  	<select name="score" class="form-control">
		  		<?php for ($i=1; $i <8 ; $i++) { 
		  		echo '<option value="'.$i.'" '.(isset($question['score'])? ($i == $question['score']?'selected':''):'').'>'.$i.'分</option>';
		  		} ?>
		  	</select>
		  	<?php foreach ($question_attr as $key => $attr): ?>
		  	<select name="<?php echo $key ?>" class="form-control" style="max-width: 160px;">
		  		<option value=""><?php echo $key ?></option>
		  		<?php foreach($attr as $k=>$v): ?>
		  		<?php if(is_array($v)): ?>
		  			<optgroup label="<?php echo $k ?>">
		  			<?php foreach($v as $k1=>$v1): ?>
		  				<option value="<?php echo $k1?>" <?php if(isset($question[$key]))echo $question[$key] == $k1?'selected':'' ?>><?php echo $v1 ?></option>
		  			<?php endforeach; ?>
		  			</optgroup>
		  		<?php else: ?>
		  		<option value="<?php echo $k?>" <?php if(isset($question[$key]))echo $question[$key] == $k?'selected':'' ?>><?php echo $v ?></option>
		  		<?php endif; ?>
		  		<?php endforeach; ?>
		  	</select>
		  	<?php endforeach; ?>
		</div>
	</div>
	
	<div class="mt-20">
		<label for="">题干</label>
		<div class="form-group" >
			<div class="alert alert-info completion-alert" style="display: none" role="alert">请在需要填空的地方输入{{填空位置}}</div>
			<textarea id="editor" name="content" style="width:800px; height:200px;"><?php echo isset($question['content'])?$question['content']:'' ?></textarea>
		</div>
	</div>
	<div class="mt-20">
		<label for="">中文题干</label>
		<div class="form-group" >
			<textarea id="content_cn" name="content_cn" style="width:800px; height:200px;"><?php echo isset($question['content_cn'])?$question['content_cn']:'' ?></textarea>
		</div>
	</div>

	<div class="mt-20">
		<label for="">补充内容</label>
		<div class="form-group" id="others-container" >
			<a href="javascript:;" class="btn btn-default others-add-btn">添加补充内容</a>
			<?php if(isset($question['others']) && $question['others']): ?>
				<?php $others = json_decode($question['others']); ?>
				<?php foreach($others as $k=>$v): ?>
				<div class="others-editor-content"><div class="t"><span><?php echo $k ?></span><a href="javascript:;" class="others-modify-btn btn ">修改</a><a href="javascript:;" class="btn others-del-btn">删除</a></div><div class="d"><?php echo $v ?></div></div>
				<?php endforeach; ?>
			<?php endif; ?>
		</div>
	</div>

	<div class="mt-20">
		<label for="">选项答案</label>
		<div class="form-group" id="answer-div">
				
		</div>
	</div>
	<div class="mt-20">
		<label for="">答案解析</label>
		<div class="form-group" >
			<textarea id="rationale" name="rationale" style="width:800px; height:200px;"><?php echo isset($question['rationale'])?$question['rationale']:'' ?></textarea>
		</div>
	</div>
	<div class="mt-20">
		<label for="">中文解析</label>
		<div class="form-group" >
			<textarea id="rationale_cn" name="rationale_cn" style="width:800px; height:200px;"><?php echo isset($question['rationale_cn'])?$question['rationale_cn']:'' ?></textarea>
		</div>
	</div>
	<div class="mt-20">
		<label for="">答题策略</label>
		<div class="form-group" >
			<textarea id="strategy" name="strategy" style="width:800px; height:200px;"><?php echo isset($question['strategy'])?$question['strategy']:'' ?></textarea>
		</div>
	</div>
	<div class="mt-20">
		<label for="">中文策略</label>
		<div class="form-group" >
			<textarea id="strategy_cn" name="strategy_cn" style="width:800px; height:200px;"><?php echo isset($question['strategy_cn'])?$question['strategy_cn']:'' ?></textarea>
		</div>
	</div>

	<div style="padding-left:70px" class="mt-20">
      <a href="javascript:;" class="btn btn-primary save">保存</a>
      <a href="<?php echo $back_url ?>" class="btn btn-default cancel">取消</a>
    </div>

	
</form>
</div>


<?php 
$js_array = array(
	'/assets/umeditor/third-party/template.min.js',
	'/assets/umeditor/umeditor.config.js',
	'/assets/umeditor/umeditor.js',
	'/assets/umeditor/lang/zh-cn/zh-cn.js',
	'/assets/ueditor/ueditor.config.js',
	'/assets/ueditor/ueditor.all.js',
	'/assets/plupload/plupload.full.min.js'
	);
$this->load->view('admin/layout/footer',array('js'=>$js_array));
?>
<script type="text/javascript">
	
	var ue = UE.getEditor('editor');
	var ue1 = UE.getEditor('rationale');
	var ue2 = UE.getEditor('strategy');
	var ue3 = UE.getEditor('rationale_cn');
	var ue4 = UE.getEditor('content_cn');
	var ue5 = UE.getEditor('strategy_cn');
	var item_editor;

	var single_html = '<div class="chose-item"><input type="radio" name="chose-item-answer"><div><input type="text"><a href="javascript:;" class="editor-a btn ">编辑器</a><a href="javascript:;" class="btn item-del-a">删除</a></div></div>';
	var multiple_html = '<div class="chose-item"><input type="checkbox" name="chose-item-answer"><div><input type="text"><a href="javascript:;" class="editor-a btn ">编辑器</a><a href="javascript:;" class="btn item-del-a">删除</a></div></div>';

	//单选题
	function single(){
		var html = '<a href="javascript:;" class="btn btn-default item-add-btn mb-10" type="single">添加选项</a> <span>填写选项时，按照【英文选项 &&& 中文选项】的格式填写，如：<span style="color:#f00">The boy &&& 男孩</span></span><div>';
		<?php if(isset($question['options']) && $question['type'] == 1):?>
			<?php $options = json_decode($question['options'],true); ?>
			<?php foreach($options as $option): ?>
				<?php if(strpos($option, '<!--html-->') === false): ?>
				html += '<div class="chose-item"><input type="radio" name="chose-item-answer"><div><input type="text" value="<?php echo htmlentities($option,ENT_QUOTES) ?>"><a href="javascript:;" class="editor-a btn ">编辑器</a><a href="javascript:;" class="btn item-del-a">删除</a></div></div>';
				<?php else: ?>
				html += '<div class="chose-item"><input type="radio" name="chose-item-answer"><div><div class="item-editor-content"><!--html--><?php echo $option ?></div><a href="javascript:;" class="editor-a btn ">编辑器</a><a href="javascript:;" class="btn item-del-a">删除</a></div></div>';
				<?php endif; ?>
			<?php endforeach; ?>
		<?php else: ?>

		for (var i = 0; i < 4; i++) {
			html += single_html;
		}
		<?php endif; ?>

		html += '</div>';

		$('#answer-div').html(html);

		<?php if(isset($question['answer']) && $question['type'] == 1): ?>
		$('input[name=chose-item-answer]').eq(<?php echo $question['answer']-1 ?>).prop('checked',true);
		<?php endif; ?>
	}
	//多选题
	function multiple(options,answer){
		var html = '<a href="javascript:;" class="btn btn-default item-add-btn mb-10" type="multiple">添加选项</a> <span>填写选项时，按照【英文选项 &&& 中文选项】的格式填写，如：<span style="color:#f00">The boy &&& 男孩</span><div>';

		<?php if(isset($question['options']) && $question['type'] == 2):?>
			<?php $options = json_decode($question['options'],true); ?>
			<?php foreach($options as $option): ?>
				<?php if(strpos($option, '<!--html-->') === false): ?>
				html += '<div class="chose-item"><input type="checkbox" name="chose-item-answer"><div><input type="text" value="<?php echo htmlentities($option,ENT_QUOTES) ?>"><a href="javascript:;" class="editor-a btn ">编辑器</a><a href="javascript:;" class="btn item-del-a">删除</a></div></div>';
				<?php else: ?>
				html += '<div class="chose-item"><input type="radio" name="chose-item-answer"><div><div class="item-editor-content"><!--html--><?php echo $option ?></div><a href="javascript:;" class="editor-a btn ">编辑器</a><a href="javascript:;" class="btn item-del-a">删除</a></div></div>';
				<?php endif; ?>
			<?php endforeach; ?>
		<?php else: ?>

		for (var i = 0; i < 4; i++) {
			html += multiple_html;
		}
		<?php endif; ?>
		
		html += '</div>';

		$('#answer-div').html(html);
		<?php if(isset($question['answer']) && $question['type'] == 2): ?>
		<?php $answers = explode(',',$question['answer']); ?>
		<?php foreach($answers as $a): ?>
		$('input[name=chose-item-answer]').eq(<?php echo $a-1 ?>).prop('checked',true);
		<?php endforeach; ?>
		<?php endif; ?>
	}
	//选图题
	function imageChose(){
		var html = '<a href="javascript:;" class="btn btn-default" id="imageChose-up-btn">上传图片</a>'
		html += '<div id="imageChose-div" style="background-color:#fff"></div>';
		$('#answer-div').html(html);
		<?php if(isset($question['options']) && $question['type'] == 4): ?>
		$('#imageChose-div').html('<img src="<?php echo $question['options'] ?>" />');
		<?php endif; ?>
		<?php if(isset($question['answer']) && $question['type'] == 4): ?>
		<?php $answers = json_decode($question['answer'],true); ?>
		<?php foreach($answers as $a): ?>
		$('#imageChose-div').append('<div class="imageChose-item" style="top:<?php echo $a['y']?>px;left:<?php echo $a['x']?>px;width:<?php echo $a['width']?>px;height:<?php echo $a['height']?>px"><a href="javascript:;" class="imageChose-item-remove glyphicon glyphicon-remove"></a></div>')
		<?php endforeach; ?>
		<?php endif; ?>
		uploadImg();
	}
	//排序题
	function sortOption(options,answer){
		
		var add = '<a href="javascript:;" class="sortOption-add-btn btn btn-default"><i class="glyphicon glyphicon-plus "></i> 添加选项</a> <span>填写选项时，按照【英文选项 &&& 中文选项】的格式填写，如：<span style="color:#f00">The boy &&& 男孩</span>';
		var html = '';

		<?php if(isset($question['options']) && $question['type'] == 5): ?>
			<?php 
				$options = json_decode($question['options'],true);
				$answers = explode(',', $question['answer']);
				$i = 0;
			?>
			<?php foreach($options as $option): ?>
			html += '<span class="sortOption-item"><input type="text" value="<?php echo $answers[$i] ?>" style="text-align:center" name="sortOption-item-num" size="3"/><input type="text" name="sortOption-item-text" value="<?php echo $option ?>"/><a href="javascript:;" class="sortOption-del-btn">删除</a><a href="javascript:;" class="sortOption-upload-btn">上传图片</a></span>'
			<?php $i++; ?>
			<?php endforeach; ?>
		<?php else: ?>
		var item = '<span class="sortOption-item"><input type="text" value="{number}" style="text-align:center" name="sortOption-item-num" size="3"/><input type="text" name="sortOption-item-text" value="{val}"/><a href="javascript:;" class="sortOption-del-btn">删除</a><a href="javascript:;" class="sortOption-upload-btn">上传图片</a></span>';
		for (var i = 1; i < 6; i++) {
			html += item.replace('{number}',i).replace('{val}','');
		}
		<?php endif; ?>
		
		html += add;
		$('#answer-div').html(html);
	}
	//填空题
	function completion(answer){
		var html = '<div><span>填写答案时，按照【英文答案 &&& 中文答案】的格式填写，如：<span style="color:#f00">The boy &&& 男孩</span></div>';
		<?php if(isset($question['answer']) && $question['type'] == 3): ?>
		<?php $answers = json_decode($question['answer'],true);?>
		<?php foreach($answers as $a): ?>
		html += '<span class="completion-span"><input type="text" name="completion_input" value="<?php echo $a?>" /><a href="javascript:;" class="glyphicon glyphicon-remove"></a></span><a href="javascript:;" class="completion-add-btn"><i class="glyphicon glyphicon-plus "></i></a>';
		<?php endforeach; ?>
		<?php else: ?>
		html += '<span class="completion-span"><input type="text" name="completion_input" /><a href="javascript:;" class="glyphicon glyphicon-remove"></a></span><a href="javascript:;" class="completion-add-btn"><i class="glyphicon glyphicon-plus "></i></a>';
		<?php endif; ?>
		$('#answer-div').html(html);
	}
	//选择题型
	function typeChange(){
		var type = $('select[name=type]').val();
		$('.completion-alert').hide();
		switch(parseInt(type)){
			case 1:
				single();
				break;
			case 2:
				multiple();
				break;
			case 3:
				$('.completion-alert').show();
				completion();
				break;
			case 4:
				imageChose();
				break;
			case 5:
				sortOption();
				break;
		}
	}

	$(function(){
		typeChange();

		//单选多选删除选项
		$('body').on('click','.item-del-a',function(){
			$(this).parent().parent().remove();
		})

		//单选多选的选项输入富文本
		$('body').on('click','.editor-a',function(){
			if(item_editor) item_editor.destroy();
			var item_index = $(this).parent().parent().index();
			var editor_html = '<textarea name="item-editor" id="item-editor" style="width:560px; height:300px;"><div class="editor-tip">请在下面框中填写<span style="color:#f00">英文选项内容</span></div><div class="editor-area-div"></div><hr class="lan-hr"/><div class="editor-tip">请在下面框中填写<span style="color:#f00">中文选项内容</span></div><div class="editor-area-div"></div></textarea>';
			var btn = '<a href="javascript:;" class="btn btn-primary item-editor-submit">提交</a>';
			var dlg = dialog('设置选项',editor_html,btn);
			
			if($(this).parent().find('.item-editor-content').length>0){
				$('#item-editor').val($(this).parent().find('.item-editor-content').html())
			}
			
			item_editor = UM.getEditor('item-editor');

			$('.item-editor-submit').on('click',function(){
				var content = item_editor.getContent();
				if(content){
				content = '<div class="item-editor-content"><!--html-->'+content+'</div><a href="javascript:;" class="editor-a btn ">编辑器</a><a href="javascript:;" class="btn item-del-a">删除</a>'

				$('#answer-div .chose-item').eq(item_index).find('div').html(content);
				}
				dlg.modal('hide');
			})
		})

		//单选多选添加一个选项
		$('body').on('click','.item-add-btn',function(){
			var html = single_html;
			if($(this).attr('type') == 'multiple') html = multiple_html;
			$(this).next().append(html)
		})

		//选图题的划定范围
		var mouse_start_x,mouse_start_y,is_moving,is_created;
		$('body').on('mousedown','#imageChose-div',function(e){
			var parent_left = $('#imageChose-div').offset().left;
			var parent_top = $('#imageChose-div').offset().top;

			if(e.target.className == 'imageChose-item') return;
			if(e.target.tagName == 'A'){
				$(e.target).parent().remove();
				return;
			}
			is_moving = true;

			mouse_start_x = e.pageX;
			mouse_start_y = e.pageY;
			var left = mouse_start_x - parent_left;
			var top = mouse_start_y - parent_top;
			$('#imageChose-div').append('<div class="imageChose-item-cache" style="top:'+top+'px;left:'+left+'px"></div>');
		})

		$('body').on('mousemove','#imageChose-div',function(e){
			if(is_moving){
				var width = e.pageX - mouse_start_x;
				var height = e.pageY - mouse_start_y;

				if(width>0 && height>0){
					$('.imageChose-item-cache').width(width);
					$('.imageChose-item-cache').height(height);
					is_created = true;
				}
			}
		})

		$('body').on('mouseup',function(e){
			$('.imageChose-item-cache').each(function(index, el) {
				if($(this).width()<40 || $(this).height()<40){
					$('.imageChose-item-cache').remove();
				}
			});

			is_moving = false;
			if(is_created) $('.imageChose-item-cache').addClass('imageChose-item').removeClass('imageChose-item-cache');
			$('.imageChose-item').html('<a href="javascript:;" class="imageChose-item-remove glyphicon glyphicon-remove"></a>');
			is_created = false;
		})

		//添加填空题答案
		$('body').on('click','.completion-add-btn',function(){
			var html = '<span class="completion-span"><input type="text" name="completion_input" /><a href="javascript:;" class="glyphicon glyphicon-remove"></a></span>';
			$(this).before(html);
		})

		//删除填空题答案
		$('body').on('click','.completion-span a',function(){
			$(this).parent().remove();
		})

		//排序题排序
		$('body').on('blur','input[name=sortOption-item-num]',function(){
			var val = parseInt($(this).val());
			if(val<1 || val > $('input[name=sortOption-item-num]').length){
				Toast.error('请填写正确的排序');
				$(this).val('');
			}
			var cf = 0;
			$('input[name=sortOption-item-num]').each(function(index, el) {
				if($(this).val() == val){
					cf ++;
					if(cf == 2){
						$(this).val('');
					}
				}
			});
		})

		//删除排序题选项
		$('body').on('click','.sortOption-del-btn',function(){
			$(this).parent().remove();
		})

		//排序题上传图片
		$('body').on('click','.sortOption-upload-btn',function(){
			
			var index = $(this).parent().index();
			var html = '<a href="javascript:;" class="btn btn-primary" id="upload-img-btn">上传图片</a><div id="tmp-img-div" index="'+index+'"></div>';
			var btn = '<a href="javascript:;" class="btn btn-primary upload-img-submit">确定</a>';
			var dlg = dialog('图片上传',html,btn);
			uploadImg('upload-img-btn','#tmp-img-div');

			$('.upload-img-submit').on('click',function(){
				if($('#tmp-img-div img').length < 1){
					alert('请上传图片！');
					return;
				}

				var img = $('#tmp-img-div img').attr('src');
				var index = $('#tmp-img-div').attr('index');
				$('.sortOption-item').eq(index).find('input[name=sortOption-item-text]').val(img);

				dlg.modal('hide');

			})
		})

		

		//添加排序题选项
		$('body').on('click','.sortOption-add-btn',function(){
			var len = $('input[name=sortOption-item-num]').length+1;
			var item = '<span class="sortOption-item"><input type="text" value="'+len+'" style="text-align:center" name="sortOption-item-num" size="3"/><input type="text" name="sortOption-item-text"/><a href="javascript:;" class="sortOption-del-btn">删除</a><a href="javascript:;" class="sortOption-upload-btn">上传图片</a></span>';
			$(this).before(item);
		})
		//改变题目题型
		$('body').on('change','select[name=type]',typeChange);

		//添加补充内容
		$('body').on('click','.others-add-btn',function(){
			if(item_editor) item_editor.destroy();
			
			var editor_html = '<input type="text" name="others-editor-title" placeholder="请填写标题" class="input mb-10 input-lg"/><textarea name="item-editor" id="item-editor" style="width:560px; height:200px;"></textarea>';
			var btn = '<a href="javascript:;" class="btn btn-primary other-editor-submit">提交</a>';
			var dlg = dialog('添加补充内容',editor_html,btn);
			
			item_editor = UM.getEditor('item-editor');

			$('.other-editor-submit').on('click',function(){
				var content = item_editor.getContent();
				if(content){
					var tt = $('input[name=others-editor-title]').val();
					content = '<div class="others-editor-content"><div class="t"><span>'+tt+'</span><a href="javascript:;" class="others-modify-btn btn ">修改</a><a href="javascript:;" class="btn others-del-btn">删除</a></div><div class="d">'+content+'</div></div>'

					$('.others-add-btn').parent().append(content);
				}
				dlg.modal('hide');
			})
		})

		//删除补充内容
		$('body').on('click','.others-del-btn',function(){
			$(this).parent().parent().remove();
		})

		//修改补充内容
		$('body').on('click','.others-modify-btn',function(){
			var o_index = $(this).parent().parent().index()-1;
			var title = $(this).parent().find('span').text();
			var content = $(this).parent().parent().find('.d').html();

			if(item_editor) item_editor.destroy();
			
			var editor_html = '<input type="text" name="others-editor-title" class="input mb-10 input-lg" placeholder="请填写标题" value="'+title+'" /><textarea name="item-editor" id="item-editor" style="width:560px; height:200px;">'+content+'</textarea>';
			var btn = '<a href="javascript:;" class="btn btn-primary other-editor-submit">提交</a>';
			var dlg = dialog('修改补充内容',editor_html,btn);
			
			item_editor = UM.getEditor('item-editor');

			$('.other-editor-submit').on('click',function(){
				var content = item_editor.getContent();
				if(content){
					var tt = $('input[name=others-editor-title]').val();
					content = '<div class="others-editor-content"><div class="t"><span>'+tt+'</span><a href="javascript:;" class="others-modify-btn btn ">修改</a><a href="javascript:;" class="btn others-del-btn">删除</a></div><div class="d">'+content+'</div></div>'

					$('.others-editor-content').eq(o_index).replaceWith(content);
				}
				dlg.modal('hide');
			})
		})

		$('body').on('click','.save',postForm);

	})

	function postForm(){
		if(!ue.getContent()){
			alert('请填写题目题干！');
			return;
		}
		if(!ue1.getContent()){
			alert('请填写答案解析！');
			return;
		}

		$slt = true;
		$('select').each(function(index, el) {
			if($(this).val() < 1) $slt = false;
		});

		if(!$slt){
			alert('请选择题目属性！');
			return;
		}

		others_form();

		var type = $('select[name=type]').val();
		var options_from_flag = false;
		switch(parseInt(type)){
			case 1:
				options_from_flag = single_form();
				break;
			case 2:
				options_from_flag = multiple_form();
				break;
			case 3:
				options_from_flag = completion_form();
				break;
			case 4:
				options_from_flag = imageChose_form();
				break;
			case 5:
				options_from_flag = sortOption_form();
				break;
		}

		if(!options_from_flag){
			return;
		}
		//return;
		Toast.loading();
		$.post('/admin/question/save',$('#question-form').serialize(),function(data){
			Toast.destory();
			var json = $.parseJSON(data);
			if(json.code == 1){
				alert('操作失败，请重试！');
				return;
			}
			Toast.success('操作成功！',function(){
				window.location = '<?php echo $back_url ?>#tr-'+json.question_id;
			})
		})



	}

	//补充内容生成FORM
	function others_form(){
		if($('.others-editor-content').length < 1) return;
		$('#others-form-div').html('');
		$('.others-editor-content').each(function(index, el) {
			$('#others-form-div').append('<textarea  name="others_form_title[]" id="others_form_title_'+index+'" >'+$(this).find('.t span').text()+'</textarea>');
			$('#others-form-div').append('<textarea name="others_form_content[]" id="others_form_content_'+index+'"></textarea>');
			$('#others_form_content_'+index).val($(this).find('.d').html());
		});
	}

	function single_form(){
		if($('.chose-item').length < 1) return false;
		$('#options-form-div').html('');
		var answer = 0;
		$('.chose-item').each(function(index, el) {
			if($(this).find('input[type=radio]').prop('checked') == true){
				answer = index+1;
				$('#options-form-div').append('<input type="text" name="answer" value="'+(index+1)+'"/>');
			}
			console.log($(this).find('div input').length);
			if($(this).find('.item-editor-content').length>0){
				$('#options-form-div').append('<textarea name="options_from_content[]" id="options_from_content_'+index+'"></textarea>');
				$('#options_from_content_'+index).html($(this).find('.item-editor-content').html());
			}else if($(this).find('div input').length > 0){
				$('#options-form-div').append('<textarea name="options_from_content[]">'+$(this).find('input[type=text]').val()+'</textarea>');
			}
		});
		if(answer<1){
			alert('请选择正确答案！');
			return false;
		}
		return true;
	}

	function multiple_form(){
		if($('.chose-item').length < 1) return false;
		$('#options-form-div').html('');
		var answer = '';
		$('.chose-item').each(function(index, el) {

			if($(this).find('input[type=checkbox]').prop('checked') == true){
				if(answer) answer += ',';
				answer += (index+1);
			}
			if($(this).find('.item-editor-content').length>0){
				$('#options-form-div').append('<textarea name="options_from_content[]" id="options_from_content_'+index+'"></textarea>');
				$('#options_from_content_'+index).html($(this).find('.item-editor-content').html());
			}else if($(this).find('input[type=text]').length > 0){
				$('#options-form-div').append('<textarea name="options_from_content[]">'+$(this).find('input[type=text]').val()+'</textarea>');
			}
		});
		if(answer){
			$('#options-form-div').append('<input type="text" name="answer" value="'+answer+'"/>');
			return true;
		}else{
			alert('请选择正确答案！');
			return false;
		}
	}

	function imageChose_form(){
		if($('#imageChose-div').length < 1) return false;
		if($('.imageChose-item').length < 1){
			alert('请在图片中画出正确区域');
			return false;
		}
		$('#options-form-div').html('');
		$('#options-form-div').append('<input type="text" name="options_from_content" value="'+$('#imageChose-div').find('img').attr('src')+'" />');
		var answer = '';
		$('.imageChose-item').each(function(index, el) {
			var area = '{"x":"'+$(this).position().left+'","y":"'+$(this).position().top+'","width":"'+$(this).width()+'","height":"'+$(this).height()+'"}';
			alert($(this).width());
			if(answer) answer += ',';
			answer += area;

		});
		$('#options-form-div').append('<textarea name="answer">['+answer+']</textarea>');
		return true;
	}

	function sortOption_form(){
		if($('.sortOption-item').length < 1) return false;
		var answer = '';
		var input_ok = true;
		$('.sortOption-item').each(function(index, el) {
			var sn = parseInt($(this).find('input[name=sortOption-item-num]').val());
			if(!sn || sn<1 || sn > $('.sortOption-item').length){
				input_ok = false;
				return;
			}
			if(answer) answer +=',';
			answer += sn;
			$('#options-form-div').append('<textarea name="options_from_content[]">'+$(this).find('input[name=sortOption-item-text]').val()+'</textarea>');
		});

		if(input_ok){
			$('#options-form-div').append('<input type="text" name="answer" value="'+answer+'"/>');
			return true;
		}else{
			alert('请设置正确的排序');
			return false;
		}
	}

	function completion_form(){
		if($('input[name=completion_input]').length < 1) return false;
		var input_ok = true;
		$('input[name=completion_input]').each(function(index, el) {
			if(!$(this).val()) input_ok = false;
			$('#options-form-div').append('<textarea  name="answer[]">'+$(this).val()+'</textarea>');
		});
		if(!input_ok){
			alert('请填写答案！');
			return false;
		}

		return true;
	}


	//图片上传
	function uploadImg(upid,container){
		if(!upid) upid = 'imageChose-up-btn';
		if(!container) container = '#imageChose-div';
		var initParam = {
	        'FilesAdded': function(up, files) {
	            up.start();
	        },
	        
	        'UploadProgress': function(up, file) {
	        	$('#'+upid+'').html('上传中:'+file.percent + "%");
	        },
	        
	        'FileUploaded': function(up, file, info) {
	        	
	        	if(info.response == 'error'){
	        		Toast.error('上传失败，请重试！');
	        	}else{
	        		var result = $.parseJSON(info.response);
	        		if(result.code != 0){
	        			Toast.error('上传失败，请重试！');
	        			return;
	        		}
	        		var html = '<img src="'+result.data.img+'"/>';
	        		$(container).html(html);
	        		$('#'+upid).html('上传图片');
	        		
	        	} 
	        },
	        'Error': function(up, err, errTip) {
	            Toast.error(errTip);
	        }
	    };

	    var uploader = new plupload.Uploader({
	        browse_button : upid, //button id
	        url : '/admin/upload/image', 
	        flash_swf_url : '/assets/js/plupload/Moxie.swf', 
	        chunk_size:'4mb', 
	        max_retries:3, 
	        auto_start: true,
	        multi_selection:false,
	        max_file_size : '100mb', 
	        filters: [{title:'只能上传图片',extensions:'jpg,png,gif,jpeg'}],
	        init:initParam
	    });  

	    uploader.init();
	}

</script>








